package com.repository;

import com.data_object.AlbumDO;
import com.mysql.jdbc.ConnectionPropertiesTransform;
import com.util.DBUtil;
import com.util.Log;
import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author yuisama
 * @date 2023/03/11 15:44
 **/
public class AlbumRepo {
    @SneakyThrows
    public AlbumDO selectOneByUidAndAid(int uid, int aid) {
        String sql = "select uid,aid,title,state from albums where uid = ? and aid = ?";
        try (Connection c = DBUtil.connection()) {
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setInt(1, uid);
                ps.setInt(2, aid);

                Log.println("执行SQL:" + ps);

                try (ResultSet rs = ps.executeQuery()) {
                    if (!rs.next()) return null;
                    return new AlbumDO(rs.getInt("aid"), rs.getInt("uid"), rs.getString("title"),
                             rs.getInt("state"));
                }
            }
        }
    }
}


